package com.example.personalizededucationsystem.controller;

import com.example.personalizededucationsystem.dto.dto.AbilityAnalysisResponseDto;
import com.example.personalizededucationsystem.dto.dto.ErrorNotebookEntryResponseDto;
import com.example.personalizededucationsystem.dto.dto.StudentAssignmentSummaryDto;
import com.example.personalizededucationsystem.model.User;
import com.example.personalizededucationsystem.service.AnalysisService;
import com.example.personalizededucationsystem.service.AssignmentService;
import com.example.personalizededucationsystem.service.PracticeService;
import com.example.personalizededucationsystem.utils.IP_VALUE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin(origins = IP_VALUE.IP)
@RestController
@RequestMapping("/api/student")
public class StudentController {

    private final PracticeService practiceService;
    private final AnalysisService analysisService; // 新增依赖
    private final AssignmentService assignmentService;

    @Autowired
    public StudentController(PracticeService practiceService, AnalysisService analysisService, AssignmentService assignmentService) {
        this.practiceService = practiceService;
        this.analysisService = analysisService;
        this.assignmentService = assignmentService;
    }

    @GetMapping("/me/error-notebook")
    @PreAuthorize("hasRole('STUDENT')")
    public ResponseEntity<List<ErrorNotebookEntryResponseDto>> getMyErrorNotebook(@AuthenticationPrincipal User currentUser, @RequestParam String subject) {
        List<ErrorNotebookEntryResponseDto> errorNotebook = practiceService.getStudentErrorNotebook(currentUser, subject)
                .stream()
                .map(ErrorNotebookEntryResponseDto::fromEntity)
                .collect(Collectors.toList());
        return ResponseEntity.ok(errorNotebook);
    }

    @GetMapping("/me/ability-analysis")
    @PreAuthorize("hasRole('STUDENT')")
    public ResponseEntity<AbilityAnalysisResponseDto> getMyAbilityAnalysis(@AuthenticationPrincipal User currentUser, @RequestParam String subject) {
        return ResponseEntity.ok(new AbilityAnalysisResponseDto(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()).format(Instant.now()), analysisService.calculateStudentMastery(currentUser, subject)));
    }

    @GetMapping("/me/assignments")
    @PreAuthorize("hasRole('STUDENT')")
    public ResponseEntity<List<StudentAssignmentSummaryDto>> getMyAssignments(@AuthenticationPrincipal User currentUser) {
        List<StudentAssignmentSummaryDto> assignments = assignmentService.getAssignmentsForStudent(currentUser)
                .stream()
                .map(StudentAssignmentSummaryDto::fromEntity)
                .collect(Collectors.toList());
        return ResponseEntity.ok(assignments);
    }
}
